module water_led
#(
    parameter CNT_MAX = 25'd24_999_999
)
(
	input                  clk,
	input                  rst_n,
	
	output reg [3:0] led
);



reg [1:0] cstate;               //现态
reg [1:0] nstate;               //次态

//状态划分
localparam state_led0 = 0;      //led0亮
localparam state_led1 = 1;      //led1亮 
localparam state_led2 = 2;      //led2亮
localparam state_led3 = 3;      //led3亮

reg [25:0] cnt = 0;             //计时器赋初值为0

//计数器模块
always@(posedge clk or negedge rst_n)begin
	if(!rst_n)
		cnt <= 1'b0;               //按下复位键，清零
	else if(cnt == CNT_MAX)          //计时器达到最大值，清零重新计数
      cnt <= 1'b0;
	else
		cnt <= cnt + 1'b1;         
end


//第一段：现态跟随次态，时序逻辑，非阻塞赋值
always@(posedge clk or negedge rst_n)begin
	if(!rst_n)
		cstate <= state_led0;      //复位键被按下，当前状态设置为led0亮
	else
		cstate <= nstate;          //下一次状态赋值给当前状态
end
		

//第二段：组合逻辑，阻塞赋值
always@(*)begin
	if(!rst_n)
		begin
			nstate = state_led0;
		end
	else
		case(cstate)
			state_led0: begin
								if(cnt == CNT_MAX)            //该状态持续时间为0.5s，0.5s后，下一次状态更改为led1亮
									nstate = state_led1;
								else
									nstate = state_led0;
							end
			state_led1: begin
								if(cnt == CNT_MAX)
									nstate = state_led2;
								else
									nstate = state_led1;
							end
			state_led2: begin
								if(cnt == CNT_MAX)
									nstate = state_led3;
								else
									nstate = state_led2;
							end
			state_led3: begin
								if(cnt == CNT_MAX)
									nstate = state_led0;
								else
									nstate = state_led3;
							end
			default:				nstate = state_led0;    
      endcase
end

//第三段：跟随状态输出
always@(posedge clk or negedge rst_n)begin
	if(!rst_n)
		led <= 4'b0001;
	else
		case(cstate)
			state_led0: led <= 4'b0001;
			state_led1: led <= 4'b0010;
			state_led2: led <= 4'b0100;
			state_led3: led <= 4'b1000;
			default   :               ;
		endcase

end

endmodule
